##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(tidyr)
library(tibble)
library(stringr)
library(gt)
library(readr)
library(modelsummary)
source('felm-summary-table.R')


files = paste0('results/',c('current-results-2008-2012.Rdata',
                                        'current-results-2012-2016.Rdata',
                                        'current-results-2016-2020.Rdata'))

load(files[1])
ModelDemSpExpReps1=ModelDemSpExpReps
ModelDemSpExpOths1=ModelDemSpExpOths
ModelDemSpExpDems1=ModelDemSpExpDems

ModelRepSpExpReps1=ModelRepSpExpReps
ModelRepSpExpOths1=ModelRepSpExpOths
ModelRepSpExpDems1=ModelRepSpExpDems


load(files[2])
ModelDemSpExpReps2=ModelDemSpExpReps
ModelDemSpExpOths2=ModelDemSpExpOths
ModelDemSpExpDems2=ModelDemSpExpDems


ModelRepSpExpReps2=ModelRepSpExpReps
ModelRepSpExpOths2=ModelRepSpExpOths
ModelRepSpExpDems2=ModelRepSpExpDems

load(files[3])
ModelDemSpExpReps3=ModelDemSpExpReps
ModelDemSpExpOths3=ModelDemSpExpOths
ModelDemSpExpDems3=ModelDemSpExpDems


ModelRepSpExpReps3=ModelRepSpExpReps
ModelRepSpExpOths3=ModelRepSpExpOths
ModelRepSpExpDems3=ModelRepSpExpDems


varlist1 = c('DemSpExpDiff_nohh' = '$\\Delta$ Dem Exp',
  'hh.d.diff' = '$\\Delta$ HH Democrats',
  'hh.n.diff' = '$\\Delta$ HH Voters',
  'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
  'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
  'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
  'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
  'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
  'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
  'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
  'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
  'MarriageDiff' = '$\\Delta$ Married',
  'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
  'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
  
  )

varlist2 = c('RepSpExpDiff_nohh' = '$\\Delta$ Rep Exp',
             'hh.r.diff' = '$\\Delta$ HH Republicans',
             'hh.n.diff' = '$\\Delta$ HH Voters',
             'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
             'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
             'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
             'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
             'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
             'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
             'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
             'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
             'MarriageDiff' = '$\\Delta$ Married',
             'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
             'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
             
)


dt = felm.summary.table(summaries = list(ModelDemSpExpReps1, ModelDemSpExpOths1, ModelDemSpExpDems1, ModelDemSpExpReps2, ModelDemSpExpOths2, ModelDemSpExpDems2, ModelDemSpExpReps3, ModelDemSpExpOths3, ModelDemSpExpDems3),
                         output='latex', fmt=3,
                        coef_map = varlist1)

rt = felm.summary.table(summaries = list(ModelRepSpExpReps1, ModelRepSpExpOths1, ModelRepSpExpDems1, ModelRepSpExpReps2, ModelRepSpExpOths2, ModelRepSpExpDems2, ModelRepSpExpReps3, ModelRepSpExpOths3, ModelRepSpExpDems3),
                         output='latex', fmt=3,
                        coef_map = varlist2)

write_file(dt, 'tables/TabS8a.tex')
write_file(rt, 'tables/TabS8b.tex')
